<?php

namespace App\Http\Controllers;

use App\Models\CompanyManagementModel;
use App\Models\ContractTypeModel;
use App\Models\DepartmentManagementModel;
use App\Models\FixedAssetsModel;
use App\Models\InvoiceCategoryModel;
use App\Models\MaterialCategoryModel;
use App\Models\MaterialInquiryModel;
use App\Models\MaterialModel;
use App\Models\PaymentMethodModel;
use App\Models\PaymentModel;
use App\Models\PaymentNatureModel;
use App\Models\ProjectLevelModel;
use App\Models\ProjectModel;
use App\Models\ProjectTypeModel;
use App\Models\PurchaseCategoryModel;
use App\Models\SettlementModel;
use App\Models\SettlementTypeModel;
use App\Models\StaffManagementModel;
use App\Models\SupplierModel;
use App\Models\UnitModel;
use App\Models\WarehouseModel;
use App\Models\DataDictionaryModel;
use Illuminate\Http\Request;

class DataController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => []]);
    }

    /**
     * Get a JWT via given credentials.
     *  获取仓库数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function warehouse()
    {

        $data = WarehouseModel::select('id','warehouse_name','warehouse_keeper','warehouse_address','warehouse_mobile','created_at')->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  员工列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function staff()
    {
        $list = array();
        $data = StaffManagementModel::select('admin_user_id','name')->get();
        foreach($data as $key=>$val){
            $list[$key]['id'] = $val['admin_user_id'];
            $list[$key]['name'] = $val['name'];
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }

    /**
     * Get a JWT via given credentials.
     *  采购类别
     * @return \Illuminate\Http\JsonResponse
     */
    public function purchase_category()
    {
        $data = PurchaseCategoryModel::select('id','purchase_category_name')->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  审核类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function review_type()
    {
        $list = array();
        $data = DataDictionaryModel::IsReview;
        for($i=0;$i<count($data);$i++){
            $list[$i]['id'] = $i;
            $list[$i]['name'] = $data[$i];
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }

    /**
     * Get a JWT via given credentials.
     *  材料列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function material()
    {
        $credentials = request(['material_name','material_code','material_alias_one','page']);

        $page = empty($credentials['page'])?0:$credentials['page'];

        $limit = 5;

        $data = MaterialModel::select(['id','material_code','material_name','material_img','material_alias_one','specifications','unit_id','material_category_id','remark'])->where(function ($query) use($credentials){
            if(!empty($credentials['material_name'])){
                $query -> where('material_name', 'like', '%'.$credentials['material_name'].'%');
            }elseif(!empty($credentials['material_code'])){
                $query -> where('material_code', 'like', '%'.$credentials['material_code'].'%');
            }elseif(!empty($credentials['material_alias_one'])){
                $query -> where('material_alias_one', 'like', '%'.$credentials['material_alias_one'].'%');
            }
        })->offset($page*$limit)->limit($limit)->get();

        foreach($data as $key=>$val){
            $data[$key]['unit_name'] = UnitModel::selectValue('id',$val['unit_id']);
            $data[$key]['material_category_name'] = MaterialCategoryModel::selectValue('id',$val['material_category_id']);
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  固定资产
     * @return \Illuminate\Http\JsonResponse
     */
    public function fixed()
    {
        $credentials = request(['assets_name','assets_code','assets_alias_one','page']);

        $page = empty($credentials['page'])?0:$credentials['page'];

        $limit = 5;

        $data = FixedAssetsModel::select(['id','assets_code','assets_name','assets_img','assets_alias_one','specifications','unit_id','purchase_category_id','purchase_category_name','remark'])->where(function ($query) use($credentials){
            if(!empty($credentials['material_name'])){
                $query -> where('material_name', 'like', '%'.$credentials['material_name'].'%');
            }elseif(!empty($credentials['material_code'])){
                $query -> where('material_code', 'like', '%'.$credentials['material_code'].'%');
            }elseif(!empty($credentials['material_alias_one'])){
                $query -> where('material_alias_one', 'like', '%'.$credentials['material_alias_one'].'%');
            }
        })->offset($page*$limit)->limit($limit)->get();

        foreach($data as $key=>$val){
            $data[$key]['unit_name'] = UnitModel::selectValue('id',$val['unit_id']);
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  供应商
     * @return \Illuminate\Http\JsonResponse
     */
    public function supplier()
    {
        $data = SupplierModel::select('id','supplier_name')->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  发票类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function invoice()
    {
        $data = InvoiceCategoryModel::select('id','invoice_name')->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  部门
     * @return \Illuminate\Http\JsonResponse
     */
    public function department(){

        $credentials = request(['company_id']);

        $data = DepartmentManagementModel::where(['company_id'=>$credentials['company_id']])->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  维修方式
     * @return \Illuminate\Http\JsonResponse
     */
    public function repair_type()
    {

        $data = DataDictionaryModel::RepairType;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }
        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }

    /**
     * Get a JWT via given credentials.
     *  结算类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function settlement_type()
    {
        $data = SettlementTypeModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  付款方式
     * @return \Illuminate\Http\JsonResponse
     */
    public function payment_type()
    {
        $data = PaymentMethodModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  付款性质
     * @return \Illuminate\Http\JsonResponse
     */
    public function payment_nature()
    {
        $data = PaymentNatureModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     *  合同类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function contract_type()
    {
        $data = ContractTypeModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

    /**
     * Get a JWT via given credentials.
     * 项目类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function project_type()
    {
        $data = ProjectTypeModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }

    /**
     * Get a JWT via given credentials.
     * 项目级别
     * @return \Illuminate\Http\JsonResponse
     */
    public function project_level()
    {
        $data = ProjectLevelModel::get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }

    /**
     * Get a JWT via given credentials.
     * 项目级别
     * @return \Illuminate\Http\JsonResponse
     */
    public function project_status()
    {
        $data = ProjectModel::selectStatusOptions();
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }

    /**
     * Get a JWT via given credentials.
     * 项目级别
     * @return \Illuminate\Http\JsonResponse
     */
    public function company(Request $request)
    {
        $group_id = $request->post('group_id');

        $data = CompanyManagementModel::where(['group_id'=>$group_id])->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }

    /**
     * Get a JWT via given credentials.
     * 项目级别
     * @return \Illuminate\Http\JsonResponse
     */
    public function purchase_type(Request $request)
    {
        $data = DataDictionaryModel::PurchaseType;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }

    /**
     * Get a JWT via given credentials.
     * 品牌
     * @return \Illuminate\Http\JsonResponse
     */
    public function brand()
    {
        $data = array();
        $list = MaterialInquiryModel::select(['id','brand'])->groupBy('brand')->having('brand','!=','')->orderBy('id','asc')->get();

        if(!empty($list)){
            foreach($list as $key=>$val){
                $data[$key]['id'] =  $val['id'];
                $data[$key]['name'] =  $val['brand'];
            }
        }
        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }

    /**
     * Get a JWT via given credentials.
     * 品牌
     * @return \Illuminate\Http\JsonResponse
     */
    public function manufacturer(Request $request)
    {
        $data = array();
        $list = MaterialInquiryModel::select(['id','manufacturer'])->groupBy('manufacturer')->having('manufacturer','!=','')->orderBy('id','asc')->get();
        if(!empty($list)){
            foreach($list as $key=>$val){
                $data[$key]['id'] =  $val['id'];
                $data[$key]['name'] =  $val['manufacturer'];
            }
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);
    }

    /**
     * Get a JWT via given credentials.
     * 订单类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function order_type(Request $request)
    {
        $data = DataDictionaryModel::OrderType;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }

    /**
     * Get a JWT via given credentials.
     * 出库类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function relation_type(Request $request)
    {
        $data = DataDictionaryModel::RelationType;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }

    /**
     * Get a JWT via given credentials.
     * 入库类型
     * @return \Illuminate\Http\JsonResponse
     */
    public function warehousing_type(Request $request)
    {
        $data = DataDictionaryModel::WarehousingType;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }

    /**
     * Get a JWT via given credentials.
     * 入库状态
     * @return \Illuminate\Http\JsonResponse
     */
    public function warehousing_status(Request $request)
    {
        $data = DataDictionaryModel::WarehousingStatus;
        $i=0;
        $list = array();
        foreach($data as $key=>$val){
            $list[$i]['id'] = $key;
            $list[$i]['name'] = $val;
            $i++;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);
    }






}
